package symbol;

import java.util.HashMap;
import java.util.Set;

public class ClassTable {
        private HashMap<String, String> fields = new HashMap<String, String>();
        private HashMap<String, MethodTable> methods = new HashMap<String, MethodTable>();
        private String className;
        public String superClass;
        
        @Override
        public String toString() {
                String r = new String();
                r+= "Class ";
                r+= className + " ";
                if(superClass != null) r += "extends "+superClass+" ";
                r+= "{\n";

                Set<String> hs = fields.keySet();
                if(fields.size() == 0) r+="<<<<NO FIELDS>>>>\n";
                for(String key : hs) {
                        r+=fields.get(key) + " " + key + " ;\n";
                }
                r+="\n";
                hs = methods.keySet();
                for(String key : hs) {
                        r+=methods.get(key).toString()+"\n";
                }               
                r+="}";         
                return r;
        }
        
        public boolean addField(String id, String type) {
                if(fields.containsKey(id)) return false;
                // field definido
                fields.put(id, type);
                return true;
        }
        
        public boolean addMethod(String id, MethodTable m) {
                if(methods.containsKey(id)) return false;
                // method definido
                methods.put(id, m);
                return true;            
        }
        
        public String getFieldType(String id) {
                return fields.get(id);
        }
        
        public MethodTable getMethodTable(String id) {
                return methods.get(id);
        }
        
        public boolean containsVar(String id){
                return fields.containsKey(id);
        }
        
        public boolean containsMethod(String id){
                return methods.containsKey(id);
        }

        public void setClassName(String className) {
                this.className = className;
        }

        public String getClassName() {
                return className;
        }
        
        public int getFieldNumber(){
                return fields.size();
        }
        
}